﻿using System;
using System.Linq;
using TLD_Vision;
using TLD_NN;
using System.Drawing;
using System.Windows.Forms;

namespace NNTest {
    class Program {
        public const int maxIterations = 1000;

        static void Main(string[] args) {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);


            Console.WriteLine("Starting");
            String trainingSetLocation = args[0];
            if (!System.IO.Directory.Exists(trainingSetLocation)) {
                Console.WriteLine("Directory doesn't exist.");
                Console.ReadKey();
                return;
            }
            Console.WriteLine("Loading testset");

            string[] files = System.IO.Directory.GetFiles(trainingSetLocation);
            double[][] trainingSet = new double[files.Length][];

            Bitmap bmp = new Bitmap(files[0]);
            Size inputSize = bmp.Size;
            bmp.Dispose();

            for (int i = 0; i < files.Length; ++i) {
                String file = files[i];
                TLD_Vision.Image img = new TLD_Vision.Image(file);
                ImageData imgData = img.GetImageData();

                double[] bipolarData = new double[img.GetHeight() * img.GetWidth()];
                for (int j = 0; j < imgData.Data.Length; ++j) {
                    bipolarData[j] = (imgData[j] & 0xFF) > 128 ? 0.5 : -0.5;
                }

                trainingSet[i] = bipolarData;
            }
                SOM som = new SOM(trainingSet[0].Length, trainingSet.Length);
                som.Train(maxIterations, trainingSet);

                int[] inputMap = som.MapInput(trainingSet);

                // Test the map
                int[] q = inputMap.Distinct().ToArray();

                Console.WriteLine(q.Length + "/" + inputMap.Length);

                if (inputMap.Length == q.Length) {
                    Console.WriteLine("Unique map!");
                } else {
                    Console.WriteLine("Non unique map!");
                }


            
            Console.ReadKey();
        }
    }
}
